using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flashes : MonoBehaviour
{
	public List<Transform> transparentObjects = new List<Transform>();

	public List<Transform> cutoutObjects = new List<Transform>();

	public float timeTillFlash = 1f;

	public float randomTimeTillFlash = 2f;

	public float flashDuration = 0.2f;

	public float minAlpha;

	public float maxAlpha = 1f;

	private void OnEnable()
	{
		StartFlashing();
	}

	private void SetIntensity(float intensity)
	{
		foreach (Transform transparentObject in transparentObjects)
		{
			transparentObject.GetComponent<Renderer>().material.SetFloat("_Alpha", intensity);
		}
		foreach (Transform cutoutObject in cutoutObjects)
		{
			cutoutObject.GetComponent<Renderer>().material.color = new Color(intensity, intensity, intensity);
		}
	}

	private void StartFlashing()
	{
		StopAllCoroutines();
		StartCoroutine(DoFlashContinuous());
	}

	private IEnumerator DoFlashContinuous()
	{
		while (true)
		{
			SetIntensity(minAlpha);
			float waitTillFlash = timeTillFlash + Random.Range(0f, randomTimeTillFlash);
			yield return new WaitForSeconds(waitTillFlash);
			float time = 0f;
			while (time < 1f)
			{
				time += RealTime.deltaTime / flashDuration;
				float intensity = 0f;
				SetIntensity(Mathf.Lerp(t: (!(time < 0.5f)) ? Mathf.Lerp(1f, 0f, (time - 0.5f) / 0.5f) : Mathf.Lerp(0f, 1f, time / 0.5f), a: minAlpha, b: maxAlpha));
				yield return null;
			}
		}
	}
}
